home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / objstr.zip / OBJSTR.PAS < prev   
Pascal/Delphi Source File  |  1991-03-06  |  9KB  |  336 lines

  1. Unit ObjStr;
  2.  
  3. { During the creation of many programs, I have been bugged by the fact that
  4.   every "standard" string declared takes up 256 bytes of memory.  If you try
  5.   to work around this by declaring 'AVariable : String[5]' or some such, you
  6.   then have to turn off Var-String checking when passing these variables to
  7.   functions and procedures expecting variables of type 'String'.  Writing
  8.   your own procedures and functions for these variables has the same draw-
  9.   back.  This is my second attempt at another solution.  The first attempt
  10.   was implemented for a graphics mode program, was much more complicated,
  11.   and was (is) too hard to maintain.  I think this unit worked out better.
  12.   As far as overhead is concerned, each instance requires 8 bytes (7 for
  13.   object fields, 1 for length byte) plus the number of chars for that type.
  14.   In other words, an instance of object type Str10 would require 18 bytes.
  15.   You have the plusses of working with objects (encapsulation, extensibilty,
  16.   ...etc.) and bypassing the Var-string checking problems that 'String[10]'
  17.   poses.  On the minus side, if you're the type who doesn't mind turning off
  18.   Var-string checking and declaring all those different string types, this
  19.   approach "wastes" 8 bytes per variable.  With those 8 bytes, however,
  20.   comes the freedom to use Str10.Val as type String.
  21.  
  22.   This unit works with TP 5.5 & 6.0 (just compile with appropriate version).
  23.   This code may be freely copied and distributed.  The user of this unit may
  24.   not hold me liable for any damages resulting from the use or misuse of
  25.   this unit.  Any modifications to this unit should be noted if modified
  26.   code is distributed.  Comments, questions, and optional monetary contribu-
  27.   tions should be made to:
  28.                              Jim Fralix
  29.                              415 Parkdale Dr.
  30.                              Apt. 5-B
  31.                              Charleston, SC  29414
  32.  
  33. }
  34.  
  35. Interface
  36.  
  37.    Const
  38.  
  39.       Zero : Byte = 0;
  40.  
  41.    Type
  42.  
  43.       StrPtr = ^String;
  44.  
  45.       StringObj = Object           { Do not Define an instance of this, as
  46.                                      Size is never defined }
  47.  
  48.          Constructor Init;                         { Allocates memory for
  49.   MUST be called before using other functions        the string }
  50.  
  51.          Procedure Read; Virtual;                  { Reads CR terminated
  52.   Ex. StringObj.Read;                                String from keyboard,
  53.                                                      truncates to max len. }
  54.  
  55.          Procedure Write; Virtual;                 { Writes String value
  56.   Ex. StringObj.Write;                               to Screen (TextMode) }
  57.  
  58.          Procedure SetTo (InStr : String); Virtual;{ Set string value equal
  59.   Ex. StringObj.SetTo (InStr);                       to InStr, truncates if
  60.                                                      necessary}
  61.  
  62.          Function Value : String; Virtual;         { Returns string value,
  63.   Ex. NormStr := StrObj.Value;                       useful in standard
  64.   Ex. Writeln ('Object String = ',StringObj.Value);  procs & funcs needing
  65.                                                      a "normal" string     }
  66.  
  67.          Destructor Done;                          { Delallocates memory   }
  68.  
  69.          Private
  70.  
  71.          S    : pointer; { To Access as String w/length Byte at index 0 }
  72.  
  73.          Size : Byte;    { Stores max length, set by init procedure }
  74.  
  75.       End;
  76.  
  77.       { THESE ARE THE USABLE OBJECTS - TO IMPLEMENT A NEW STRING SIZE, JUST
  78.         DEFINE IT AND CODE THE APPROPRIATE 'INIT' CONSTRUCTOR }
  79.  
  80.       Str2 = Object (StringObj)
  81.          Constructor Init;  { Sets Size to 2, allocates memory }
  82.       End;
  83.  
  84.       Str3 = Object (StringObj)
  85.          Constructor Init;  { Sets Size to 3, allocates memory }
  86.       End;
  87.  
  88.       Str4 = Object (StringObj)
  89.          Constructor Init;  { Sets Size to 4, allocates memory }
  90.       End;
  91.  
  92.       Str5 = Object (StringObj)
  93.          Constructor Init;  { You should have the idea by now! }
  94.       End;
  95.  
  96.       Str7 = Object (StringObj)
  97.          Constructor Init;
  98.       End;
  99.  
  100.       Str8 = Object (StringObj)
  101.          Constructor Init;
  102.       End;
  103.  
  104.       Str10 = Object (StringObj)
  105.          Constructor Init;
  106.       End;
  107.  
  108.       Str12 = Object (StringObj)
  109.          Constructor Init;
  110.       End;
  111.  
  112.       Str15 = Object (StringObj)
  113.          Constructor Init;
  114.       End;
  115.  
  116.       Str18 = Object (StringObj)
  117.          Constructor Init;
  118.       End;
  119.  
  120.       Str20 = Object (StringObj)
  121.          Constructor Init;
  122.       End;
  123.  
  124.       Str25 = Object (StringObj)
  125.          Constructor Init;
  126.       End;
  127.  
  128.       Str30 = Object (StringObj)
  129.          Constructor Init;
  130.       End;
  131.  
  132.       Str35 = Object (StringObj)
  133.          Constructor Init;
  134.       End;
  135.  
  136.       Str40 = Object (StringObj)
  137.          Constructor Init;
  138.       End;
  139.  
  140.       Str50 = Object (StringObj)
  141.          Constructor Init;
  142.       End;
  143.  
  144.       Str60 = Object (StringObj)
  145.          Constructor Init;
  146.       End;
  147.  
  148.       Str70 = Object (StringObj)
  149.          Constructor Init;
  150.       End;
  151.  
  152.       Str80 = Object (StringObj)
  153.          Constructor Init;
  154.       End;
  155.  
  156.       Str90 = Object (StringObj)
  157.          Constructor Init;
  158.       End;
  159.  
  160.       Str100 = Object (StringObj)
  161.          Constructor Init;
  162.       End;
  163.  
  164.    Var
  165.  
  166.       TempStr : String;    { Sometimes you gotta have a standard string! }
  167.  
  168. Implementation
  169.  
  170.    Constructor StringObj.Init;  { Called by the other INIT Procedures }
  171.    Begin
  172.       GetMem (S, Size+1);          { Allocate mem. for length byte & chars }
  173.       Move (Zero, S^, 1);          { Init to null String (length byte of 0) }
  174.    End;
  175.  
  176.    Procedure StringObj.Read;    { Read CR terminated string from input }
  177.    Begin
  178.       Readln (TempStr);
  179.       Move (TempStr, StrPtr(S)^, Size+1);
  180.       If (Length (TempStr) > Size) then     { need to truncate }
  181.          Move (Size, StrPtr(S)^, 1);
  182.    End;
  183.  
  184.    Procedure StringObj.Write;   { WritesString at current cursor location }
  185.    Begin
  186.       System.Write (StrPtr(S)^);
  187.    End;
  188.  
  189.    Procedure StringObj.SetTo (InStr : String);  { Sets value of StringObj }
  190.    Begin
  191.       Move (InStr, StrPtr(S)^, Size+1);
  192.       If (Length(InStr) > Size) then        { need to truncate }
  193.          Move (StrPtr(S)^, Size, 1);
  194.    End;
  195.  
  196.    Function StringObj.Value : String;  { Returns string value of StringObj }
  197.    Begin
  198.       Value := StrPtr(S)^;
  199.    End;
  200.  
  201.    Destructor StringObj.Done;     { Releases Memory }
  202.    Begin
  203.       FreeMem (S, Size+1);
  204.    End;
  205.  
  206.    Constructor Str2.Init;
  207.    Begin
  208.       Size := 2;
  209.       StringObj.Init;
  210.    End;
  211.  
  212.    Constructor Str3.Init;
  213.    Begin
  214.       Size := 3;
  215.       StringObj.Init;
  216.    End;
  217.  
  218.    Constructor Str4.Init;
  219.    Begin
  220.       Size := 4;
  221.       StringObj.Init;
  222.    End;
  223.  
  224.    Constructor Str5.Init;
  225.    Begin
  226.       Size := 5;
  227.       StringObj.Init;
  228.    End;
  229.  
  230.    Constructor Str7.Init;
  231.    Begin
  232.       Size := 7;
  233.       StringObj.Init;
  234.    End;
  235.  
  236.    Constructor Str8.Init;
  237.    Begin
  238.       Size := 8;
  239.       StringObj.Init;
  240.    End;
  241.  
  242.    Constructor Str10.Init;
  243.    Begin
  244.       Size := 10;
  245.       StringObj.Init;
  246.    End;
  247.  
  248.    Constructor Str12.Init;
  249.    Begin
  250.       Size := 12;
  251.       StringObj.Init;
  252.    End;
  253.  
  254.    Constructor Str15.Init;
  255.    Begin
  256.       Size := 15;
  257.       StringObj.Init;
  258.    End;
  259.  
  260.    Constructor Str18.Init;
  261.    Begin
  262.       Size := 18;
  263.       StringObj.Init;
  264.    End;
  265.  
  266.    Constructor Str20.Init;
  267.    Begin
  268.       Size := 20;
  269.       StringObj.Init;
  270.    End;
  271.  
  272.    Constructor Str25.Init;
  273.    Begin
  274.       Size := 25;
  275.       StringObj.Init;
  276.    End;
  277.  
  278.    Constructor Str30.Init;
  279.    Begin
  280.       Size := 30;
  281.       StringObj.Init;
  282.    End;
  283.  
  284.    Constructor Str35.Init;
  285.    Begin
  286.       Size := 35;
  287.       StringObj.Init;
  288.    End;
  289.  
  290.    Constructor Str40.Init;
  291.    Begin
  292.       Size := 40;
  293.       StringObj.Init;
  294.    End;
  295.  
  296.    Constructor Str50.Init;
  297.    Begin
  298.       Size := 50;
  299.       StringObj.Init;
  300.    End;
  301.  
  302.    Constructor Str60.Init;
  303.    Begin
  304.       Size := 60;
  305.       StringObj.Init;
  306.    End;
  307.  
  308.    Constructor Str70.Init;
  309.    Begin
  310.       Size := 70;
  311.       StringObj.Init;
  312.    End;
  313.  
  314.    Constructor Str80.Init;
  315.    Begin
  316.       Size := 80;
  317.       StringObj.Init;
  318.    End;
  319.  
  320.    Constructor Str90.Init;
  321.    Begin
  322.       Size := 90;
  323.       StringObj.Init;
  324.    End;
  325.  
  326.    Constructor Str100.Init;
  327.    Begin
  328.       Size := 100;
  329.       StringObj.Init;
  330.    End;
  331. {
  332. Begin
  333.  
  334.    Writeln ('Size of ObjStr is ',SizeOf(StringObj):1);
  335. }
  336. End.